package com.dongzili.demo.common.config.base;

import cn.hutool.extra.spring.SpringUtil;
import com.dongzili.demo.common.config.es.EsUtils;
import com.dongzili.demo.common.config.mybatisplus.MybatisPlusUtil;
import com.dongzili.demo.common.utils.ReflectionCache;
import org.dromara.easyes.core.kernel.BaseEsMapper;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import java.io.Serializable;

public class BaseService<T> {

    private final Class<T> clazz;

    private BaseService(Class<T> clazz) {
        this.clazz = clazz;
    }

    public static <T> BaseService<T> getService(Class<T> clazz) {
        return new BaseService<>(clazz);
    }

    /**
     * 插入或更新数据
     */
    public T insert(T t) {
        PlatformTransactionManager platformTransactionManager = SpringUtil.getBean(PlatformTransactionManager.class);
        TransactionDefinition transactionDefinition = SpringUtil.getBean(TransactionDefinition.class);
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            MybatisPlusUtil.getMapper(clazz).insert(t);
            EsUtils.getMapper(clazz).insert(t);
            platformTransactionManager.commit(transaction);
        }catch (Exception e){
            transaction.setRollbackOnly();
            throw e;
        }
        return t;
    }

    /**
     * 根据id更新数据
     */
    public boolean update(T t) {
        PlatformTransactionManager platformTransactionManager = SpringUtil.getBean(PlatformTransactionManager.class);
        TransactionDefinition transactionDefinition = SpringUtil.getBean(TransactionDefinition.class);
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            if (ReflectionCache.getPrimaryValue(t) == null){
                throw new IllegalArgumentException("id不能为空");
            }
            MybatisPlusUtil.getMapper(clazz).updateById(t);
            BaseEsMapper<T> esMapper = EsUtils.getMapper(clazz);
            T oldData = esMapper.selectById(ReflectionCache.getPrimaryValue(t));
            if (oldData!= null) {
                esMapper.updateById(t);
            }else{
                esMapper.insert(t);
            }
            platformTransactionManager.commit(transaction);
        }catch (Exception e){
            transaction.setRollbackOnly();
            throw e;
        }
        return true;
    }

    /**
     * 根据id删除数据
     */
    public boolean deleteById(Class<T> clazz, Serializable id) {
        PlatformTransactionManager platformTransactionManager = SpringUtil.getBean(PlatformTransactionManager.class);
        TransactionDefinition transactionDefinition = SpringUtil.getBean(TransactionDefinition.class);
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            if (id == null){
                throw new IllegalArgumentException("id不能为空");
            }
            MybatisPlusUtil.getMapper(clazz).deleteById(id);
            EsUtils.getMapper(clazz).deleteById(id);
            platformTransactionManager.commit(transaction);
        }catch (Exception e){
            transaction.setRollbackOnly();
            throw e;
        }
        return true;
    }

}
